local rate = {
  [1] = {--战士
    STR = {
      AP = 2,
      Block = 0.05
    },
    AGI = {
      F_AP = 1,
      Crit = 0.05,
      Dodge = 0.05,
      Armor = 2
    }
	},
	[2] = {--圣骑士
    STR = {
      AP = 2,
      Block = 0.05
    },
    AGI = {
      Crit = 0.05,
      Dodge = 0.05,
      Armor = 2
    },
    INT = {
      Crit = 0.017,
    }
  },
	[3] = {--猎人
    STR = {
      AP = 1
    },
    AGI = {
      N_AP = 1,
      F_AP = 2,
      Crit = 0.019,
      Dodge = 0.038,
      Armor = 2
    }
  },
  [4] = {--盗贼
    STR = {
      AP = 1
    },
    AGI = {
      N_AP = 1,
      F_AP = 1,
      Crit = 0.034,
      Dodge = 0.07,
      Armor = 2
    }
  },
  [5] = {--牧师
    STR = {
      AP = 1
    },
    AGI = {
      Dodge = 0.05,
      Armor = 2
    },
    INT = {
      Crit = 0.017,
    }
  },
  [7] = {--萨满
    STR = {
      AP = 2,
      Block = 0.05
    },
    AGI = {
      Crit = 0.05,
      Dodge = 0.05,
      Armor = 2
    },
    INT = {
      Crit = 0.017,
    }
  },
  [8] = {--法师
    STR = {
      AP = 1
    },
    AGI = {
      Dodge = 0.05,
      Armor = 2
    },
    INT = {
      Crit = 0.017,
    }
  },
  [9] = {--术士
    STR = {
      AP = 1
    },
    AGI = {
      Dodge = 0.05,
      Armor = 2
    },
    INT = {
      Crit = 0.017,
    }
  },
	[11] = {--德鲁伊
    STR = {
      AP = 2.4
    },
    AGI = {
      N_AP = 1,
      Crit = 0.05,
      Dodge = 0.05,
      Armor = 2
    },
    INT = {
      Crit = 0.017,
    }
	}
}

function formatPercentage(value)
  value = tostring(value)
  return string.format("%.2f",value)..'%'
end

GameTooltip:HookScript("OnTooltipSetItem", function(tooltip)

  -- 判断是否开启
  if _G["AddonUiDB"].profile.fuEqStats ~= true then
    return
  end

  local _,_,classCode = UnitClass('player')
  local _,ItemLink = tooltip:GetItem()
  if(ItemLink == nil) then return end
  
  local stats = GetItemStats(ItemLink)
  local race, raceEn = UnitRace("player")
  
  if(stats == nil) then return end

  local STR = stats['ITEM_MOD_STRENGTH_SHORT']
  local AGI = stats['ITEM_MOD_AGILITY_SHORT']
  local STA = stats['ITEM_MOD_STAMINA_SHORT']
  local INT = stats['ITEM_MOD_INTELLECT_SHORT']
  local SPI = stats['ITEM_MOD_SPIRIT_SHORT']

  if (STR == nil and AGI == nil and STA == nil and INT == nil and SPI == nil ) then return end
  local current = rate[classCode]

  tooltip:AddLine("|cFFD9D919属性换算|r")
  
  if(STR ~= nil)
  then
    local text = '力量: '
    if current['STR'].AP then text = text..'|cFF70DB93近战攻强 '..STR * current['STR'].AP..' |r | ' end
    if current['STR'].Block then text = text..'|cFF70DB93格挡伤害 '..STR * current['STR'].Block..' |r | ' end
    text = string.sub(text,1,-3)
    tooltip:AddLine(text,0,1,0)
  end

  if(AGI ~= nil)
  then
    local text = '敏捷: '
    if current['AGI'].N_AP then text = text..'|cFF70DB93近战攻强 '..AGI * current['AGI'].N_AP..' |r | ' end
    if current['AGI'].F_AP then text = text..'|cFF70DB93远程攻强 '..AGI * current['AGI'].F_AP..' |r | ' end
    if current['AGI'].Crit then text = text..'|cFF70DB93暴击 '..formatPercentage(AGI * current['AGI'].Crit)..' |r | ' end
    if current['AGI'].Dodge then text = text..'|cFF70DB93闪避 '..formatPercentage(AGI * current['AGI'].Dodge)..' |r | ' end
    if current['AGI'].Armor then text = text..'|cFF70DB93护甲 '..AGI * current['AGI'].Armor..' |r | ' end
    text = string.sub(text,1,-3)
    tooltip:AddLine(text,0,1,0)
  end

  if(STA ~= nil)
  then
    local HP = '|cFF70DB93生命 '..STA * (race == '牛头人' and 10.5 or 10)..'|r'
    tooltip:AddLine('耐力: '..HP,0,1,0)
  end

  if(INT ~= nil)
  then
    local text = '智力: '
    text = text..'|cFF70DB93法力 '..(INT * 15)..'|r | '
    if current['INT'] and current['INT'].Crit then text = text..'|cFF70DB93法暴 '..formatPercentage(INT * current['INT'].Crit)..' |r | ' end
    text = string.sub(text,1,-3)
    tooltip:AddLine(text,0,1,0)
  end

  -- if(SPI ~= nil)
  -- then
  --   local MP = '2秒回蓝 '..INT * 15
  --   tooltip:AddLine('精神:'..MP,0,1,0)
  -- end
  
  tooltip:Show()
end)
